package com.dha.asynctask;

import java.util.ArrayList;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.dha.caodangcongnghe.R;
import com.dha.model.DiemHocKyModel;
import com.dha.variable.XemDiemVariable;

public class XemDiemThongKetAsyncTask extends AsyncTask<Void, String, Void> {

	private Activity activity;
	private ProgressDialog dialog;
	private ArrayList<DiemHocKyModel> arr;
	private ListView lvDanhSachDiem;
	private ArrayAdapter<DiemHocKyModel> adapter;
	private String maSinhVien;

	// Lay ra diem cuoi khoa
	private String diemCBCTL, xepLoai, soTCTL;
	private TextView tv_DiemTongKetCuoiKhoa_TBCTL,
			tv_DiemTongKetCuoiKhoa_STCTL, tv_DiemTongKetCuoiKhoa_XepLoai;

	public XemDiemThongKetAsyncTask(Activity activity, String maSinhVien) {
		super();
		this.activity = activity;
		this.maSinhVien = maSinhVien;

		arr = new ArrayList<DiemHocKyModel>();
		lvDanhSachDiem = (ListView) this.activity
				.findViewById(R.id.lv_DS_Diem_hocKy);
		adapter = new ArrayAdapter<DiemHocKyModel>(activity,
				android.R.layout.simple_list_item_1, arr);

		lvDanhSachDiem.setAdapter(adapter);
		
		tv_DiemTongKetCuoiKhoa_TBCTL = (TextView) this.activity
				.findViewById(R.id.tv_DiemTongKetCuoiKhoa_TBCTL);
		tv_DiemTongKetCuoiKhoa_STCTL = (TextView) this.activity
				.findViewById(R.id.tv_DiemTongKetCuoiKhoa_STCTL);
		tv_DiemTongKetCuoiKhoa_XepLoai = (TextView) this.activity
				.findViewById(R.id.tv_DiemTongKetCuoiKhoa_XepLoai);
	}

	@Override
	protected void onPreExecute() {
		dialog = ProgressDialog.show(activity, "Vui lòng chờ",
				"Đang tải bảng điểm tổng kết về");
		super.onPreExecute();
	}

	@Override
	protected void onPostExecute(Void result) {
		// TODO Auto-generated method stub
		tv_DiemTongKetCuoiKhoa_STCTL.setText(soTCTL);
		tv_DiemTongKetCuoiKhoa_TBCTL.setText(diemCBCTL);
		tv_DiemTongKetCuoiKhoa_XepLoai.setText(xepLoai);
		dialog.dismiss();
		super.onPostExecute(result);
	}

	@Override
	protected Void doInBackground(Void... params) {
		Log.i("hehe", "vao back");
		SoapObject request;
		PropertyInfo maSinhVienInfo = new PropertyInfo();
		maSinhVienInfo.setName("MaSinhVien");
		maSinhVienInfo.setValue(maSinhVien);
		SoapSerializationEnvelope envelope = null;
		HttpTransportSE httpTransportSE;
		try {
			request = new SoapObject(XemDiemVariable.NAME_SPACE,
					XemDiemVariable.METHOD_NAME_XEMDIEMTONGKETHOCKY);

			request.addProperty(maSinhVienInfo);

			envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.setOutputSoapObject(request);

			httpTransportSE = new HttpTransportSE(XemDiemVariable.URL);
			httpTransportSE
					.call(XemDiemVariable.NAME_SPACE
							+ XemDiemVariable.METHOD_NAME_XEMDIEMTONGKETHOCKY,
							envelope);

			Vector<SoapObject> respone = (Vector<SoapObject>) envelope
					.getResponse();

			if (respone != null) {
				for (SoapObject so : respone) {
					publishProgress(so.getProperty("diemTBCHT").toString(), so
							.getProperty("hocKy").toString(),
							so.getProperty("soTinChiDangKy").toString(), so
									.getProperty("xepLoaiHocKy").toString());
				}
			}

		} catch (java.lang.ClassCastException ex) {
			// neu la 1 hoc ky, no cast sai, ra exceop ti
			// ==> vao day xu ly
			try {
				SoapObject so = (SoapObject) envelope.getResponse();

				publishProgress(so.getProperty("diemTBCHT").toString(), so
						.getProperty("hocKy").toString(),
						so.getProperty("soTinChiDangKy").toString(), so
								.getProperty("xepLoaiHocKy").toString());

			} catch (SoapFault e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (Exception e) {
			Log.i("hehe", e.toString());
		}
		// Xử lý tải về điểm tổng kết cuối khóa
		try {
			request = new SoapObject(XemDiemVariable.NAME_SPACE,
					XemDiemVariable.METHOD_NAME_XEMDIEMTONGKETKHOAHOC);

			request.addProperty(maSinhVienInfo);

			envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.setOutputSoapObject(request);

			httpTransportSE = new HttpTransportSE(XemDiemVariable.URL);
			httpTransportSE.call(XemDiemVariable.NAME_SPACE
					+ XemDiemVariable.METHOD_NAME_XEMDIEMTONGKETKHOAHOC,
					envelope);
			try {
				SoapObject so = (SoapObject) envelope.getResponse();
				
				diemCBCTL = so.getProperty("diemTBCTL").toString();
				xepLoai = so.getProperty("xepLoai").toString();
				soTCTL = so.getProperty("soTinChiTichLuy").toString();
				// publishProgress(so.getProperty("diemTBCTL").toString(),
				// so.getProperty("soTinChiTichLuy").toString(), so
				// .getProperty("xepLoai").toString());

			} catch (SoapFault e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

		return null;
	}

	@Override
	protected void onProgressUpdate(String... values) {
		String diemTBCTL = values[0].toString();
		String hocKy = values[1].toString();
		String soTC = values[2].toString();
		String xepLoai = values[3].toString();

		DiemHocKyModel diem = new DiemHocKyModel(hocKy, soTC, diemTBCTL,
				xepLoai);

		arr.add(diem);

		adapter.notifyDataSetChanged();

		super.onProgressUpdate(values);
	}

}
